package org.consistenthash;

import java.util.SortedMap;
import java.util.TreeMap;



public class ConsistentHashNoVirtual {

    public static void main(String[] args) {
        //step1 初始化：把服务器ip的哈希值对应到哈希环上
        //定义服务器IP
        String[] tomcatServers = new String[]{"123.111.0.0","123.101.3.1","111.20.35.2","123.98.26.3"};

        SortedMap<Integer,String> hashServerMap =  new TreeMap<>();

        for (String tomcatServer : tomcatServers) {
            //求出每一个Ip对应的 Hash值
            int serverHash = Math.abs(tomcatServer.hashCode());
            //存储Hash值和IP的对应关系
            hashServerMap.put(serverHash,tomcatServer);
        }

        //step2 针对客户端IP求出hash值
        // 定义客户端IP
        String[] clients = new String[]{"10.78.12.3","113.25.63.1","126.12.3.8"};
        for (String client : clients) {
            int clientHash = Math.abs(client.hashCode());
            //step3 针对客户端 找到能够处理当前客户端请求的服务器（hash环上顺时针最近）
            //根据客户端ip的hash值去找出哪一个服务器节点能够处理
            SortedMap<Integer, String> integerStringSortedMap = hashServerMap.tailMap(clientHash);
            if(integerStringSortedMap.isEmpty()){
                //取hash环上顺时针第一条数据
                Integer firstKey = hashServerMap.firstKey();
                System.out.println("==========>>>>客户端：" + client + " 被路由到服务器：" + hashServerMap.get(firstKey));
            }else {
                Integer firstKey = integerStringSortedMap.firstKey();
                System.out.println("==========>>>>客户端：" + client + " 被路由到服务器：" + hashServerMap.get(firstKey));
            }

        }

    }

}
